package com.leancloud.circle;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewTreeObserver;

import java.lang.reflect.Method;

public class WindowsUtil {

    public static int getStateBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public static boolean isNavigationBarShow(Activity context) {
        DisplayMetrics dm = new DisplayMetrics();
        Display display = context.getWindowManager().getDefaultDisplay();
        display.getMetrics(dm);
        int screenHeight = dm.heightPixels;
        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealMetrics(realDisplayMetrics);
        } else {
            Class c;
            try {
                c = Class.forName("android.view.Display");
                Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
                method.invoke(display, realDisplayMetrics);
            } catch (Exception e) {
                realDisplayMetrics.setToDefaults();
                e.printStackTrace();
            }
        }
        int screenRealHeight = realDisplayMetrics.heightPixels;
        return (screenRealHeight - screenHeight) > 0;
    }

    public static ViewTreeObserver.OnGlobalLayoutListener getGlobalLayoutListener(final View decorView, final View contentView, final Activity context, final View inputLayout) {
        return new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                decorView.getWindowVisibleDisplayFrame(r);
                int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
                int navigationBarHeight = (int) context.getResources().getDimension(R.dimen.dp_49);
                int diff;
                if (WindowsUtil.isNavigationBarShow(context)) {
                    diff = height - r.bottom - navigationBarHeight + WindowsUtil.getStateBarHeight(context);
                } else {
                    diff = height - r.bottom - navigationBarHeight;
                }

                if (diff > navigationBarHeight) {
                    if (contentView.getPaddingBottom() != diff) {
                        contentView.setPadding(0, 0, 0, diff);
                    }
                } else {
                    if (contentView.getPaddingBottom() != 0) {
                        contentView.setPadding(0, 0, 0, 0);
                        inputLayout.setVisibility(View.GONE);
                    }
                }
            }
        };
    }

    public static ViewTreeObserver.OnGlobalLayoutListener getNoNavigateGlobalLayoutListener(final View decorView, final View contentView, final Activity context, final View inputLayout) {
        return new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                decorView.getWindowVisibleDisplayFrame(r);
                int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
                int navigationBarHeight = (int) context.getResources().getDimension(R.dimen.dp_49);
                int diff;
                if (WindowsUtil.isNavigationBarShow(context)) {
                    diff = height - r.bottom  + WindowsUtil.getStateBarHeight(context);
                } else {
                    diff = height - r.bottom ;
                }

                if (diff > navigationBarHeight) {
                    if (contentView.getPaddingBottom() != diff) {
                        contentView.setPadding(0, 0, 0, diff);
                    }
                } else {
                    if (contentView.getPaddingBottom() != 0) {
                        contentView.setPadding(0, 0, 0, 0);
                        inputLayout.setVisibility(View.GONE);
                    }
                }
            }
        };
    }
}
